package com.qnw.CardGroupManagement;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.qnw.CardGroupManagement.TiaoJian;
import com.qnw.CardGroupManagement.TiaoJianAdapter;
import com.ygo.feihua.Gongju;
import com.ygo.feihua.R;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import android.support.v7.widget.Toolbar;
import android.os.Handler;
import android.os.Message;
import com.ygo.feihua.util.*;
import com.xiaomi.mistatistic.sdk.*;
import com.ygo.feihua.Management.*;

public class B5 extends AppCompatActivity implements OnClickListener
{
	public static List<TiaoJian> tiaoJianList=new ArrayList<TiaoJian>();
	private String[] kb;
	private Map<String,Integer> jiLu=new TreeMap();
	private ListView tiaoJianLV;
	private Button kscs;
	private EditText edcs;
	
	Gongju gj;
	DialogUtils du;
	
	private int cs; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.b5_layout);
		
		Toolbar tool=(Toolbar) findViewById(R.id.toolbar);
		
		gj=Gongju.getdx(this);
		du=DialogUtils.getdx(this);
		
		gj.cshToolbar(tool,"测试卡手率");
		
		tiaoJianList.clear();
		String wzkb=getIntent().getStringExtra("wzkb");
		String[] sssss=wzkb.split("\n");
		kb = new String[sssss.length - 1];
		for (int i=0;i < sssss.length - 1;i++)
		{
			kb[i] = sssss[i + 1];
		}
		Set eeeee=new TreeSet();
		for (int i=0;i < kb.length;i++)
		{
			if (!eeeee.contains(kb[i]))
			{
				eeeee.add(kb[i]);
			}
		}
		Object[] s=eeeee.toArray();
		for (int i=0;i < eeeee.size();i++)
		{
			TiaoJian tiaoJian=new TiaoJian(s[i].toString(), -3);
			tiaoJianList.add(tiaoJian);
		}
		TiaoJianAdapter adapter=new TiaoJianAdapter(B5.this, tiaoJianList);
		tiaoJianLV = (ListView)findViewById(R.id.b5_lv_tj);
		tiaoJianLV.setAdapter(adapter);
		kscs = (Button)findViewById(R.id.b5_bu_kscs);
		edcs = (EditText)findViewById(R.id.b5_ed_cscs);

		kscs.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View p1)
	{
		switch (p1.getId())
		{
			case R.id.b5_bu_kscs:
				{
					if (TextUtils.isEmpty(edcs.getText().toString())){
						gj.show("测试次数不能为空");
						return; 
					}

					//测试次数
					cs=Integer.parseInt(edcs.getText().toString());
					du.dialogj1(false,"","测试中,请稍等");
					new Thread(new Runnable(){

							@Override
							public void run()
							{

								jiLu.put("先攻",0);
								jiLu.put("第二回合",0);
								jiLu.put("第三回合",0);
								jiLu.put("第四回合",0);
								jiLu.put("第五回合",0);
								for (int i=0;i < cs;i++)
								{
									Map<String,Integer> m=new TreeMap();
									Map<String,Integer> m2=new TreeMap();
									for (int l=0;l < tiaoJianList.size();l++)
									{
										if (!m.containsKey(tiaoJianList.get(l).getName()))
										{

											m.put(tiaoJianList.get(l).getName(), tiaoJianList.get(l).getZs());
											m2.put(tiaoJianList.get(l).getName(), 0);
										}
									}	
									//数组转化为数列
									List<String> kbb=Arrays.asList(kb);
									//随机打乱数列
									Collections.shuffle(kbb);
									for (int j=0;j < 10;j++)
									{
										m2.put(kbb.get(j), m2.get(kbb.get(j)) + 1);
										if (j == 4){
											if (isTrue(m, m2)){
												jiLu.put("先攻", jiLu.get("先攻") + 1);
											}else{
												continue;
											}
										}else if (j == 5){
											if (isTrue(m, m2)){
												jiLu.put("第二回合", jiLu.get("第二回合") + 1);
											}else{
												continue;
											}
										}else if (j == 6){
											if (isTrue(m, m2)){
												jiLu.put("第三回合", jiLu.get("第三回合") + 1);
											}else{
												continue;
											}
										}else if (j == 7){
											if (isTrue(m, m2)){
												jiLu.put("第四回合", jiLu.get("第四回合") + 1);
											}else{
												continue;
											}
										}else if (j == 8){
											if (isTrue(m, m2)){
												jiLu.put("第五回合", jiLu.get("第五回合") + 1);
											}else{
												continue;
											}
										}else if (j > 8){
											break;
										}
									}
								}
								handle.sendEmptyMessage(0);						
								// TODO: Implement this method
							}
						}).start();
					break;
				}
		}
	}

	@Override
	protected void onResume(){
		// TODO: Implement this method
		super.onResume();
		SdkInitManagement.getDx().initMiStats();
		MiStatInterface.recordPageStart(this, "卡手率测试");
	}

	@Override
	protected void onPause(){
		// TODO: Implement this method
		super.onPause();
		MiStatInterface.recordPageEnd();
	}
	
	
	
	private boolean isTrue(Map<String,Integer> m, Map<String,Integer> m2)
	{
		//返回Map中的键的Set
		Set values = m.keySet();   
		//遍历集
		for (Object object : values) 
		{
			//转为String
			String s=object.toString();
			
			if (m.get(s) == 10){
				if (m2.get(s) != 0){
					return false;
				}
			}else if (m.get(s) < 0){
				if (m2.get(s) > -m.get(s)){
					return false;
				}
			}else{
				if (m2.get(s) < m.get(s)){
					return false;
				}
			}
		}
		return true;
	}
	
	private String dvl(String s,int cs)
	{
		double css=cs;
		double jieguo=jiLu.get(s)/css*100;
		String ss=String.valueOf(jieguo);
		if(ss.length()>5){
			ss=ss.substring(0,5);
		}

		return ss;
	}
	
	
	Handler handle=new Handler(){

		@Override
		public void handleMessage(Message msg)
		{
			// TODO: Implement this method
			super.handleMessage(msg);

			switch(msg.what){
				case 0:
					du.dis();
					View[] vd =du.dialogt1(true,"测试结果","先攻\t"+dvl("先攻",cs)+"%\n"+
										   "第二回合\t"+dvl("第二回合",cs)+"%\n"+
										   "第三回合\t"+dvl("第三回合",cs)+"%\n"+
										   "第四回合\t"+dvl("第四回合",cs)+"%\n"+
										   "第五回合\t"+dvl("第五回合",cs)+"%\n");
					Button b1 =(Button) vd[0];
					b1.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								du.dis();
								// TODO: Implement this method
							}
						});
					break;		
			}
		}
	};
	
}
